package com.medialog.am.db;

	import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.medialog.am.vo.ConnectionInfo;
import com.medialog.am.vo.ProcessedData;
	public class Dao {
		static Logger logger = Logger.getLogger(Dao.class);
		
		private ConnectionInfo connectionInfo;
		public Dao(){
			
		}

		public List<ConnectionInfo> getServerInfoList() throws Exception{
			logger.debug("GetServerInfoList entry");
			DbConnector dbConn = new DbConnector();
			List<ConnectionInfo>svrInfoList = new ArrayList<ConnectionInfo>();
			dbConn.setCon();
			for(int i =0; i<3; i++) {
				try {
				String sql = 
						"SELECT S.SVRNUM, S.HOST, S.SVRID, S.SVRPW, S.PORTNUM, S.SERIALNO, C.CPUCRITICAL, C.MEMORYCRITICAL, C.HDDCRITICAL,"
						+ "C.CPUMAJOR, C.MEMORYMAJOR, C.HDDMAJOR,C.CPUMINOR, C.MEMORYMINOR, C.HDDMINOR from TB_LST_SERVER_TBOSS S"
						+ " JOIN TB_LST_CRITICAL_TBOSS C ON (S.SERIALNO = C.SERIALNO)";
				
				dbConn.setPsmt(null);
				dbConn.setRs(null); 	
				dbConn.setPsmt(dbConn.getCon().prepareStatement(sql));
				dbConn.setRs(dbConn.getPsmt().executeQuery());
			
				while(dbConn.getRs().next()){
					connectionInfo = new ConnectionInfo();
					connectionInfo.setSvrNum(dbConn.getRs().getInt("svrNum"));
					connectionInfo.setHost(dbConn.getRs().getString("host"));
					connectionInfo.setSvrId(dbConn.getRs().getString("svrId"));
					connectionInfo.setSvrPw(dbConn.getRs().getString("svrPw"));
					connectionInfo.setPortNum(dbConn.getRs().getInt("portnum"));
					connectionInfo.setSerialNo(dbConn.getRs().getString("serialno"));
					connectionInfo.setCpuCritical(dbConn.getRs().getInt("cpuCritical"));
					connectionInfo.setCpuMajor(dbConn.getRs().getInt("cpuMajor"));
					connectionInfo.setCpuMinor(dbConn.getRs().getInt("cpuMinor"));
					connectionInfo.setMemoryCritical(dbConn.getRs().getInt("memoryCritical"));
					connectionInfo.setMemoryMajor(dbConn.getRs().getInt("memoryMajor"));
					connectionInfo.setMemoryMinor(dbConn.getRs().getInt("memoryMinor"));
					connectionInfo.setHddCritical(dbConn.getRs().getInt("hddCritical"));
					connectionInfo.setHddMajor(dbConn.getRs().getInt("hddMajor"));
					connectionInfo.setHddMinor(dbConn.getRs().getInt("hddMinor"));
					svrInfoList.add(connectionInfo);
					
					logger.debug("Server Information-[svrNum :"+connectionInfo.getSvrNum()+" host :"+connectionInfo.getHost()+" svrId :"+connectionInfo.getSvrId()
								+" svrPw :"+connectionInfo.getSvrPw()+" portNum :"+connectionInfo.getPortNum()+" serialNo :"+connectionInfo.getSerialNo()
								+" cpuCritical :"+connectionInfo.getCpuCritical()+" cpuMajor :"+connectionInfo.getCpuMajor()+" cpuMinor :"
								+connectionInfo.getCpuMinor()+" memCritical :"+connectionInfo.getMemoryCritical()+" memMajor :"+connectionInfo.getMemoryMajor()
								+" memMinor :"+connectionInfo.getMemoryMinor()+" hddCritical :"+connectionInfo.getHddCritical()+" hddMajor :"
								+connectionInfo.getHddMajor()+" hddMinor :"+connectionInfo.getHddMinor()+"]");
				}
				break;
				} catch(Exception e){
					if(i==2){
						logger.error((i+1)+"'s acquisition failure information of assets managed server");
						throw e;
					} else {
						try {
							logger.debug((i+1)+"'s retry for get information of assets managed server");
							Thread.sleep(5000);
						} catch(Exception ee){
						}
					}
				} finally{
					
					if (dbConn.getRs() != null) { 
						dbConn.getRs().close();
					}
					if (dbConn.getPsmt() != null) {
						dbConn.getPsmt().close();
					}
					if (dbConn.getCon() != null) {
						dbConn.getCon().close();
					}
				}
			}
			logger.debug("getServerInfoList exit");
			return svrInfoList;
		}
	
	
		public int saveCollectedResourceList(List<ProcessedData> processedDataList, String collectTime) throws Exception{
			logger.info("saveCollectedResourceList entry");
			
			DbConnector dbConn = new DbConnector();
			
	
			String sql = "INSERT INTO TB_LST_RESOURCECOLLECTOR_TBOSS VALUES(?,?,?,?,?,?,?,?)";
		
			int batchResult[] = null;
			int successRow = 0;
			for(int i =0; i<3; i++) {
				dbConn.setCon();
				dbConn.setPsmt(dbConn.getCon().prepareStatement(sql));
				try{
					int count =0;
					for (ProcessedData processedData : processedDataList) {
						dbConn.getPsmt().setString(1,collectTime);
						dbConn.getPsmt().setInt(2,processedData.getSvrNum());
						dbConn.getPsmt().setFloat(3,processedData.getCpuRate());
						dbConn.getPsmt().setFloat(4,processedData.getMemRate());
						dbConn.getPsmt().setFloat(5,processedData.getHddRate());
						dbConn.getPsmt().setString(6,processedData.getAlarmCpu());
						dbConn.getPsmt().setString(7,processedData.getAlarmMem());
						dbConn.getPsmt().setString(8,processedData.getAlarmHdd());
					
						dbConn.getPsmt().addBatch();
						count++;
					
					
						if(count%10000 == 0){
							dbConn.getPsmt().executeBatch();
						}
			
						 	logger.debug("processedData-["+"collectTime :"+collectTime+"svrNum :"+processedData.getSvrNum()+" CpuRate :"+processedData.getCpuRate()
							+" MemRate :"+processedData.getMemRate()+" HddRate :"+processedData.getHddRate()+" AlarmCpu :"+processedData.getAlarmCpu()+" AlarmMem :"+
							processedData.getAlarmMem()+" AlarmHdd :"+processedData.getAlarmHdd()+"]");
					}
					
					batchResult = dbConn.getPsmt().executeBatch();
					
					for(int j=0; i<batchResult.length; i++) {
							successRow += batchResult[j];
					}
					break;
				}catch(Exception e){
					if(i==2) {
						logger.debug((i+1)+"'s Insert failure of ResourceCollector Data");
						throw e;
					} else{
						try {
						logger.debug((i+1)+"'s retry for Insert ResourceCollector Data");
						Thread.sleep(5000);
						} catch(Exception ee) {
						}
					}
				}finally{
					if(dbConn.getPsmt() != null)
						dbConn.getPsmt().close();
					if(dbConn.getCon() != null)
						dbConn.getCon().close();
				}
			}
			
			logger.debug("saveCollectedResourceList exit");
			return successRow;
		}
	}

